﻿using System;
using System.Linq;
using System.Linq.Expressions;
using Sharp.Portal.Other;

namespace Sharp.Portal.Mvc.List
{
    public partial class SharpListHelper<TEntity>
    {
        /// <summary>
        /// 浮点数
        /// </summary>
        internal static readonly Type[] FloatType =
        {
            typeof(decimal),  typeof(float) , typeof(double)
        };
        /// <summary>
        /// 整数
        /// </summary>
        internal static readonly Type[] IntgerType =
        {
            typeof(int),typeof(long),typeof(short)
        };
        

        /// <summary>
        /// 增加显示列
        /// </summary>
        /// <param name="path">属性</param>
        /// <returns>ColumnItem 对象</returns>
        protected virtual ColumnItem AddColumn<TProperty>(Expression<Func<TEntity, TProperty>> path)
        {
            return AddColumn(path, "", 0);
        }

        /// <summary>
        /// 增加显示列
        /// </summary>
        /// <param name="path">属性</param>
        /// <param name="width"></param>
        /// <returns>ColumnItem 对象</returns>
        protected virtual ColumnItem AddColumn<TProperty>(Expression<Func<TEntity, TProperty>> path, int width)
        {
            return AddColumn(path, "", width);
        }
        /// <summary>
        /// 增加显示列
        /// </summary>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="path">属性</param>
        /// <param name="title"></param>
        /// <returns>ColumnItem 对象</returns>
        protected virtual ColumnItem AddColumn<TProperty>(Expression<Func<TEntity, TProperty>> path, string title)
        {
            return AddColumn(path, title, 0);
        }


        /// <summary>
        /// 增加显示列
        /// </summary>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="path">属性</param>
        /// <param name="title"></param>
        /// <param name="width"></param>
        /// <returns>ColumnItem 对象</returns>
        protected virtual ColumnItem AddColumn<TProperty>(Expression<Func<TEntity, TProperty>> path, string title, int width)
        {
            string propertyname;
            LambdaHelper.TryParsePath(path.Body, out propertyname);

            if (Parent.ColumnItems.Any(x => x.Name == propertyname))
            {
                return Parent.ColumnItems.First(x => x.Name == propertyname);
            }

            if (title.IsNullOrEmpty())
            {
                if (!LambdaHelper.TryParseDisplay(path.Body, out title))
                {
                    title = string.Empty;
                }
            }

            var column = AddColumn(propertyname, title, width);
            column.Bind = path.Body;
            column.DataType = path.Body.Type;

            if (path.Body.Type.IsAssignableFrom(typeof(DateTime)))
            {
                column.Feature |= ColumnFeature.DateColumn;
                column.Format = "Y-m-d H:i:s";
            }
            if (FloatType.Any(x => x == path.Body.Type))
            {
                column.Feature |= ColumnFeature.NumberColumn;
                column.Align = Alignment.Right;
            }
            if (IntgerType.Any(x => x == path.Body.Type))
            {
                column.Align = Alignment.Right;
            }


            if (column.MasterDataCategory.IsNullOrEmpty())
            {
                string category;
                if (LambdaHelper.TryParseCodeSort(path.Body, out category))
                {
                    column.MasterDataCategory = category;
                }
            }


            return column;
        }



        /// <summary>
        /// 增加显示列
        /// </summary>
        /// <param name="propertyname">属性</param>
        /// <param name="title"></param>
        /// <param name="width"></param>
        /// <returns>ColumnItem 对象</returns>
        protected ColumnItem AddColumn(string propertyname, string title, int width)
        {
            return AddColumn(propertyname, title, width, string.Empty, Alignment.Left);
        }


        /// <summary>
        /// 插入显示列
        /// </summary>
        /// <param name="propertyname">属性</param>
        /// <param name="text">显示标题，默认为空串；为空串时，表示该列不显示</param>
        /// <param name="width">列宽，默认100, 传入0时，使用默认值</param>
        /// <param name="header">组合列头标题，默认为空串</param>
        /// <param name="alignment">对齐</param>
        /// <returns>ColumnItem 对象</returns>
        private ColumnItem AddColumn(string propertyname, string text, int width, string header, Alignment alignment)
        {
            ColumnItem column = new ColumnItem()
            {
                Name = propertyname
            };

            if (text.Length > 0)
                column.Text = text;
            if (width > 0)
                column.Width = width;
            column.ModuleId = Parent.ModuleId;
            column.Align = alignment;
            column.GroupedHeader = header;
            if (column.DataType == null)
                column.DataType = typeof(string);


            if (column.IsEntitled())
            {
                Parent.ColumnItems.Add(column);
            }

            return column;
        }

     
    }
}
